<?php
/**
 *
 * 主控制器
 *
 * @author		Tata <windzb@qq.com>
 */

class Controller {

	//全局唯一实例
	private static $instance;

	//加载对象
	public $load;

	public $uid;

	/*卖家*/
	public $tb_uid;
	public $tb_nick;
	public $top_session;

	/*买家*/
	public $buyer_uid;
	public $buyer_nick;
	public $buyer_nick_mix;

	/*AJAX返回参数*/
	public $callback;
	public $ajax_err; //返回的错误


	/*
    * ------------------------------------------------------
    *  构建函数
    * ------------------------------------------------------
    */
	public function __construct()
	{
		global $context;

		//全局唯一实例
		self::$instance =& $this;


		if(!empty($_REQUEST["callback"]))
		{
			$this->callback = $_REQUEST["callback"];
		}
		//买家信息
		//$this->buyer_info_init();


		//初始化加载
		$this->init_load();


		if($_SESSION && $_SESSION['id']) $this->uid = $_SESSION['id'];
		if($_SESSION && $_SESSION['tb_uid']) $this->tb_uid = $_SESSION['tb_uid'];
		if($_SESSION && $_SESSION['tb_nick']) $this->tb_nick = $_SESSION['tb_nick'];
		if($_SESSION && $_SESSION['top_session']) $this->top_session = $_SESSION['top_session'];
		//当前卖家信息
		/*if($_SESSION['tb_nick'] && $_SESSION['tb_uid'] && $_SESSION['top_session']){
			$this->tb_uid = $_SESSION['tb_uid'];
			$this->tb_nick = $_SESSION['tb_nick'];
		}
		else{
			//登录过期
			$this->ajax_err = array(2,'登录过期,请重新登录授权.',2,'<a href="'.LIMIT_AUTH_URL.'" target="_blank">请<strong>【点击这里】</strong>在新页面重新登录应用后<strong>【返回本页面】</strong>继续操作>>.<a/>');
		}*/


	}

	/*
    * ------------------------------------------------------
    *  全局唯一实例
    * ------------------------------------------------------
    */
	public static function &get_instance()
	{
		return self::$instance;
	}

	/*
    * ------------------------------------------------------
    *  默认
    * ------------------------------------------------------
    */
	public function index()
	{
		header("Location: ".$_SERVER['HTTP_HOST']);
	}

	/*
    * ------------------------------------------------------
    *  初始化
    * ------------------------------------------------------
    */
	public function init_load()
	{
		require_once CORE_PATH.'/Loader.php';
		$this->load = new Loader();
	}

	/*
  * ------------------------------------------------------
  *  JSONP 返回 旧
  * ------------------------------------------------------
  */
	public function ajax_msg($msg=0,$submsg=0,$data="{}")
	{
		$json_array = array(
			'msg'=>$msg,
			'submsg'=>$submsg,
			'ooo'=>$data
		);
		$str = $this->callback.'('.json_encode($json_array).')';
		//$appLog->info($str);
		header('KissyIoDataType:jsonp');
		//header("Content-type: text/html; charset=gb2312"); //本地IE需要此项
		echo ($str);
	}


	/*
  * ------------------------------------------------------
  *  ajax 返回 新
  * ------------------------------------------------------
  */
	public function ajax($data='',$err='',$type ='json')
	{
		if($this->ajax_err){
			$err = $this->ajax_err;
		}

		if(!empty($err)){
			if(is_object($err)){
				$err = array(
					'code'=>$err->code,
					'msg'=>$err->msg,
					'sub_code'=>empty($err->sub_code) ? 0 : $err->sub_code,
					'sub_msg'=>empty($err->sub_msg) ? "" : $err->sub_msg
				);
			}else{
				$err[2] = empty($err[2]) ? 0 : $err[2];
				$err[3] = empty($err[3]) ? '' : $err[3];
				$err = array(
					'code'=>$err[0],
					'msg'=>$err[1],
					'sub_code'=>$err[2],
					'sub_msg'=>$err[3]
				);
			}
		}

		$json_array = array(
			'data'=>$data,
			'err'=>$err
		);

		//header("Content-type: text/html; charset=gb2312"); // TODO 注意TAE要修改字符编码为 gb2312
		//header('KissyIoDataType:json');
		echo (json_encode($json_array));
		return false;
	}


}